/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package IU;

import Logica.Carrera;
import Logica.Fachada;
import Logica.Hipodromo;
import Logica.Jornada;
import Logica.Participante;
import Utilidades.ManejoFechas;
import Utilidades.Observable;
import Utilidades.Observador;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author jarzarello
 */
public class CarreraGanador extends javax.swing.JDialog implements Observador {

    private final Hipodromo hs = PanelPrincipal.getInstancia().getHipodromoSeleccionado();
    private final ManejoFechas mf = new ManejoFechas();
    private Carrera carreraActual;
    private Jornada jornadaHoy;
    
    public Hipodromo getHipodromo() {
        return hs;
    }

    public ManejoFechas getManejoFechas() {
        return mf;
    }

    public Carrera getCarreraActual() {
        return carreraActual;
    }

    public void setCarreraActual(Carrera carreraActual) {
        this.carreraActual = carreraActual;
    }

    public Jornada getJornadaHoy() {
        return jornadaHoy;
    }

    public void setJornadaHoy(Jornada jornadaHoy) {
        this.jornadaHoy = jornadaHoy;
    }
    
    /**
     * Creates new form CarreraGanador
     */
    public CarreraGanador(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Fachada.getInstancia().agregar(this);
        cargarCarrera();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNumeroCarrera = new javax.swing.JLabel();
        lblNombreCarrera = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        lblCaballosParticipantes = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstCaballosParticipantes = new javax.swing.JList();
        btnCerrar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        lblNumero = new javax.swing.JLabel();
        lblNombre = new javax.swing.JLabel();
        lblMonto = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblNumeroCarrera.setText("N° Carrera:");

        lblNombreCarrera.setText("Nombre:");

        jLabel1.setText("Monto Total Jugado:");

        lblCaballosParticipantes.setText("Lista de caballos participantes:");

        lstCaballosParticipantes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstCaballosParticipantes);

        btnCerrar.setText("Cancelar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        jButton1.setText("Seleccionar Ganador");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCerrar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCaballosParticipantes)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(lblNombreCarrera)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(lblNombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(lblNumeroCarrera)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(lblNumero, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblMonto, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNumeroCarrera)
                    .addComponent(lblNumero))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombreCarrera)
                    .addComponent(lblNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblMonto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCaballosParticipantes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCerrar)
                    .addComponent(jButton1))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        // TODO add your handling code here:
        cerrarVentana();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        seleccionarGanador();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblCaballosParticipantes;
    private javax.swing.JLabel lblMonto;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblNombreCarrera;
    private javax.swing.JLabel lblNumero;
    private javax.swing.JLabel lblNumeroCarrera;
    private javax.swing.JList lstCaballosParticipantes;
    // End of variables declaration//GEN-END:variables

    private void cerrarVentana() {
        int opcion = JOptionPane.showConfirmDialog(null, "¿ Desea salir sin elegir un ganador de la carrera ?");
        if(opcion == JOptionPane.YES_OPTION){
            dispose();
        }
    }

    private void cargarCarrera() {
        ArrayList<String> participantes = new ArrayList();
        
        Date fechaHoy = this.getManejoFechas().formatoFecha(new Date());
        this.setJornadaHoy(hs.buscarJornada(fechaHoy));//CARGO JORNADA EN VARIABLE
        this.setCarreraActual(this.getJornadaHoy().buscarCerrada());//CARGO CARRERA CERRADA EN VARIABLE

        if(this.getCarreraActual() == null){
            JOptionPane.showMessageDialog(null, "No hay carreras para asignar ganador.");
            this.dispose();
        }else{
            lblNumero.setText(String.valueOf(this.getCarreraActual().getNumero()));
            lblNombre.setText(this.getCarreraActual().getNombre());
            for(Participante p:carreraActual.getParticipantes()){
                participantes.add(formatearParticipantes(p));
            }
            lstCaballosParticipantes.setListData(participantes.toArray());
        }
    }

    private void seleccionarGanador() {
      
    String participante = lstCaballosParticipantes.getSelectedValue().toString();

        if (participante == null) {
            JOptionPane.showMessageDialog(null, "No se seleccionó ningún caballo");
        } else {
            String separado[] = participante.split(" - ");
            int opcion = JOptionPane.showConfirmDialog(null, "¿ Desea seleccionar a " + separado[0] + " como ganador ?");
                if(opcion == JOptionPane.YES_OPTION){               
                    this.carreraActual.setGanador(this.carreraActual.buscarParticpante(separado[0]));
                    this.carreraActual.setEstado(3);
                    cargarCarrera();
                }
            }
    }
    
    private String formatearParticipantes (Participante p) {
        return "Número: " + String.valueOf(p.getNumero()) + " - Nombre: " + 
                p.getCaballo().getNombre()+ " - Dividendo: " + String.valueOf(p.getDividendo()) + " - Monto Total apostado: ";
    }

    @Override
    public void actualizar(Observable origen, Object param) {
        if(param.equals(Fachada.Evento.CA_FINALIZADA))
            cargarCarrera();
    }

}
